#define _CRT_SECURE_NO_WARNINGS 1


#include <climits>
#include <iostream>
using namespace std;

class Date
{
public:
    bool is_leap(int year)
    {
        return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
    }
    int GetmonthDay(int year, int month)
    {
        static int monthDay[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
        return monthDay[month - 1] + (month == 2 && is_leap(year) ? 1 : 0);
    }
    Date()
    {
        int extday;
        scanf("%d%d%d%d", &_year, &_month, &_day, &extday);
        _day += extday;
        while (_day > GetmonthDay(_year, _month))
        {
            _day -= GetmonthDay(_year, _month++);
            if (_month == 13)
            {
                _year++;
                _month = 1;
            }
        }
    }
    void Display()
    {
        printf("%04d-%02d-%02d\n", _year, _month, _day);
    }

private:
    int _year;
    int _month;
    int _day;
};
int main()
{
    int n;
    scanf("%d", &n);
    Date data[n];
    for (int i = 0; i < n; i++)
    {
        data[i].Display();
    }
    return 0;
}